package com.tonyodev.fetchapp.fetch2fileserver.database;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;

import com.tonyodev.fetch2core.MutableExtras;
import com.tonyodev.fetch2fileserver.database.FileResourceInfo;
import com.tonyodev.fetch2fileserver.database.FileResourceInfoDao;
import com.tonyodev.fetch2fileserver.database.FileResourceInfoDatabase;
import com.tonyodev.fetchapp.Data;
import com.tonyodev.fetchapp.Utils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import junit.framework.TestCase;

import static com.tonyodev.fetchapp.ability.FileServerAbility.CONTENT_PATH;
import static org.junit.Assert.*;

public class FetchFileResourceInfoDatabaseTest extends TestCase {
    private Context appContext;
    private FileResourceInfoDatabase fileResourceInfoDatabase;
    private FileResourceInfoDao fileResourceInfoDao;
    @Before
    public void setUp() throws Exception {
        appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        final String FETCH_NAMESPACE = "fileServerActivity";
        fileResourceInfoDatabase = new FileResourceInfoDatabase(appContext, FETCH_NAMESPACE);
        fileResourceInfoDao = fileResourceInfoDatabase.getInstance();
    }

    public void tearDown() throws Exception {

    }

    public void testInsert() {
        fileResourceInfoDao.deleteAll();
        final FileResourceInfo fileResourceInfo=new FileResourceInfo();
        fileResourceInfo.setId(new Date().getTime());
        final String md5 = UUID.randomUUID().toString();
        final String testFilePath = Data.getSaveDir(appContext) + "/source/" + CONTENT_PATH;
        final File file = Utils.createFile(testFilePath);
        final ResourceManager resourceManager = appContext.getResourceManager();
        final RawFileEntry rawFileEntry = resourceManager.getRawFileEntry("resources/rawfile/test_file.db");
        long readBytes = 0;
        try {
            InputStream inputStream = new BufferedInputStream(rawFileEntry.openRawFile());
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
            byte[] buffer = new byte[1024];
            int read;
            while (((read = inputStream.read(buffer, 0, 1024)) != -1)) {
                readBytes += read;
                outputStream.write(buffer, 0, read);
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        final MutableExtras extras = new MutableExtras();
        extras.putLong("contentLength", readBytes);
        extras.putString("contentName", file.getName());
        extras.putString("contentSampleKey", "contentSampleText");
        fileResourceInfo.setExtras(extras.toExtras().toJSONString());
        fileResourceInfo.setName(file.getName());
        fileResourceInfo.setLength(file.length());
        fileResourceInfo.setFile(file.getPath());
        fileResourceInfo.setMd5(md5);
        fileResourceInfoDao.insert(fileResourceInfo);
        List<FileResourceInfo> fileResourceInfos=fileResourceInfoDao.get();
        assertNotNull(fileResourceInfos);
    }

}